﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Account_kcgl_kcy : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        db.proc_CleanClassAnn(0); //-----------清除往年的考场安排-------------
        var ExamRes = (from p in db.proc_GetHaveAllotExamInfo()
                       select p).ToList();
        if (ExamRes.Count() > 0)
        {
            Button1.Enabled = false;
        }

        var ProRes = (from p in db.proc_GetSubProManNum()
                      select p).ToList();
        GridView2.DataSource = ProRes;
        GridView2.DataBind();
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 得到该控件
        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (e.NewPageIndex == -3)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        //得到新的值
        theGrid.PageIndex = newPageIndex;
        BindData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        db.proc_CleanClassAnn(1);
        if (db.proc_GetMaxSumSeat() == 1)
        {
            int classNo = 1001;

            var result = (from p in db.proc_GetBkgxNowYear()
                          select p).ToList().First();
            string ZcYear = result.dYear;
            var examRes = (from p in db.proc_GetExamPlace()
                           where p.classNo == Convert.ToString(classNo).Substring(1, 3)
                           select p).ToList();
            if (examRes.Count < 1)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('公共考场不够，请添加公共考场后再行分配！')</script>");
                return;
            }
            var resultStu = (from p in db.proc_SetExamGetStuInfo()
                             orderby p.proOrder
                             select p).ToList();
            int seatNo = 1;
            foreach (proc_SetExamGetStuInfoResult item in resultStu)
            {
                db.proc_SetZkzh(item.wbh, examRes.First().classNo, seatNo);
                var SubRes = (from p in db.proc_GetSubjectByWbh(item.wbh)
                              select p).ToList();
                int SubjectNo = 1;
                foreach (proc_GetSubjectByWbhResult items in SubRes)
                {
                    string ZyclassNo = examRes.First().classNo;
                    int ZyseatNo = seatNo;
                    var examplaceRes = (from p in db.proc_GetSubClassBySubNum(items.subNum)
                                        //where p.isallot == 0
                                        select p).ToList();
                    bool flag = false;     ////----标记专用考场是否剩余
                    for (int i = 0; i < examplaceRes.Count(); i++)
                    {

                        int maxNum = Convert.ToInt32(examplaceRes[i].maxNum);
                        int haveAllotNum = Convert.ToInt32(examplaceRes[i].haveAllotNum);
                        if (haveAllotNum >= maxNum)
                        {
                            continue;
                        }
                        flag = true;
                        ZyseatNo = haveAllotNum + 1;
                        ZyclassNo = examplaceRes[i].classNo;
                        if (haveAllotNum + 1 >= maxNum)
                        {
                            db.proc_UpdateSubClassIsallotBySC(examplaceRes[i].classNo, 1, maxNum);
                            break;
                        }
                        else
                        {
                            db.proc_UpdateSubClassIsallotBySC(examplaceRes[i].classNo, 0, haveAllotNum + 1);
                            break;
                        }
                    }

                    if (examplaceRes.Count > 0 && flag == false)
                    {
                        db.proc_CleanClassAnn(1);
                        string alertInfo = "<script>alert('用于考" + items.subName + "科目的专用考场不够，请添加此类专用考场后再行分配！')</script>";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "error", alertInfo);
                        return;
                    }
                    db.proc_AddClassAnn(ZyclassNo, items.subNum, item.wbh, ZyseatNo, SubjectNo);
                    SubjectNo++;
                }
                seatNo++;
                if (seatNo > examRes.First().maxNum)
                {
                    seatNo = 1;
                    classNo++;
                    examRes = (from p in db.proc_GetExamPlace()
                               where p.classNo == Convert.ToString(classNo).Substring(1, 3)
                               select p).ToList();
                    while (examRes.Count() < 1)
                    {
                        db.proc_CleanClassAnn(1);
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('考场不够，请添加考场后再行分配！')</script>");
                        return;
                    }
                }
            }
            BindData();
            ClientScript.RegisterClientScriptBlock(this.GetType(), "success", "<script>alert('考场安排完成！');window.location='kcgl.aspx'</script>");
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "success", "<script>alert('公共考场不够，请添加公用考场后再行分配！')</script>");
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            var res = from p in db.classAnn
                      select p;
            db.classAnn.DeleteAllOnSubmit(res);
            db.SubmitChanges();
            BindData();
            Button1.Enabled = true;
            ClientScript.RegisterClientScriptBlock(this.GetType(), "success", "<script>alert('操作成功！')</script>");
        }
        catch (Exception ex)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('操作失败，请重试！')</script>");
        }
    }
}